package org.yjh.dao;

import org.apache.ibatis.annotations.*;
import org.yjh.pojo.Goods;
import org.yjh.pojo.User;

import java.util.List;


@Mapper
public interface GoodsDao {

    // 查询 7天内数据
    @Select("select * from goods where hidden=0 and DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= date(date) ")
    public List<Goods> getListGoodsAll();

    // 根据商品类型查询
    @Select("select * from goods where hidden=0 and gtype = #{gtype}")
    public List<Goods> getListGoods(@Param("gtype") String gtype);

    // 根据商品名称查询
    @Select("select * from goods where hidden=0 and  gname like CONCAT('%',#{gname},'%') ")
    public List<Goods> getGoodsByGname(Goods g);

    @Select("<script>" +
                "select * from goods where hidden=0 " +
                "<if test=' gid !=0 ' > and gid=#{gid} </if>" +
                "<if test=' gname !=null and gname != \"\" ' > and gname like CONCAT('%',#{gname},'%') </if>" +
                "<if test=' state !=999' > and state=#{state} </if>" +
            "</script>")
    public List<Goods> getGoodsAsConditions(Goods g);

    @Select("<script>" +
                "select * from goods where hidden=0 and uid = #{uid} " +
                "<if test=' gid !=0 ' > and gid=#{gid} </if>" +
                "<if test=' gname !=null and gname != \"\" ' > and gname like CONCAT('%',#{gname},'%') </if>" +
                "<if test=' state !=999 ' > and state=#{state} </if>" +
            "</script>")
    public List<Goods> getGoodsAsConditionsOfUser(Goods g);

    @Select("select * from goods where hidden=0 and gid = #{gid}")
    public Goods getGoodsAsId(Goods g);

//    @Delete(
//            "<script>" +
//                "delete from goods where hidden=0 and " +
//                "<foreach collection=\"array\" open=\"and \" separator=\" or \" item=\"gid\">\n" +
//                    "gid = #{gid}" +
//                "</foreach>" +
//            "</script>")
    @Delete("delete from goods where gid = #{gid} " )
    public void deleteGoods(@Param("User") User u, @Param("gid") int gid);

    @Select("insert into goods(uid,gname,price,detailed,date,gtype,state,url) " +
            "values(#{uid},#{gname},#{price},#{detailed},#{date},#{gtype},1,#{url})")
    public void setGoods(Goods g);

    @Update("update Goods set  gname=#{gname}, price=#{price},detailed=#{detailed} where gid=#{gid}")
    public void upDateGoods(Goods g);


    @Select("<script>" +
                "select * from goods where 1 = 1 " +
                "<foreach collection=\"array\" open=\"and \" separator=\" or \" item=\"gid\">\n" +
                "gid = #{gid}" +
                "</foreach>" +
            "</script>")
    public List<Goods> getGids(String[] s);

    @Update("update Goods set hidden=1 where gid=#{gid}")
    public void setHidden(@Param("gid") int gid);

    /**
     * 根据uid去判断收藏表中有收藏的gid,然后查询商品表
     */
    @Select("select * from Goods where hidden=0 and gid in ( select gid from collections where uid = #{uid} )")
    public List<Goods> getGoodsForCollections(Goods g);
}
